package com.hmdp.config;

import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.UserDTO;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Map;

import static com.hmdp.utils.RedisConstants.LOGIN_USER_KEY;

public class LoginInterceptor implements HandlerInterceptor {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public void RefreshTokenInterceptor(StringRedisTemplate stringRedisTemplate){
        this.stringRedisTemplate=stringRedisTemplate;
    }
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception{
//        //获取session
//        HttpSession session=httpServletRequest.getSession();
//        //获取用户信息
//        Object user=session.getAttribute("user");
//        System.out.println(user);
//        if(user == null){
//            httpServletResponse.setStatus(401);
//            return false;
//        }
//        UserHolder.saveUser((UserDTO) user);
        if(UserHolder.getUser() == null){
            httpServletResponse.setStatus(401);
            return false;
        }
        return true;

    }
}
